package tt.dz.biz.dao.user;

import java.util.List;
import org.springframework.stereotype.Repository;
import tt.dz.biz.dao.BaseDAO;
import tt.dz.common.LogUtil;
import tt.dz.vo.OfflineAccountVO;

@Repository("offlineAccountDAO")
public class OfflineAccountDAO
  extends BaseDAO
{
  private static final String INSERT_OFFLINE_ACCOUNT = "insert into ms_offline_account(card_no,user_id,merchant_code,card_type,card_bin,area_code,check_code,card_cost,acct_bal,bat_no,init_date,init_oper,state,open_oper,open_date,valid_date,last_date,remark)  values(:cardNo,:userId,:merchantCode,:cardType,:cardBin,:areaCode,:checkCode,:cardCost,:acctBal,:batNo,:initDate,:initOper,:state,:openOper,:openDate,:validDate,:lastDate,:remark )";
  private static final String LIST_ALL_OFFLINE_ACCOUNT = "select  a.card_no,a.user_id,a.merchant_code, a.card_type,a.card_bin,a.area_code,a.check_code,a.card_cost,a.acct_bal,a.bat_no,a.init_date,a.init_oper,a.state,a.open_oper,a.open_date,a.valid_date,a.last_date,a.remark  from ms_offline_account as a ";
  private static final String UPDATE_OFFLINE_ACCOUNT = "update  ms_offline_account set user_id = :userId,card_type = :cardType,card_bin = :cardBin,area_code = :areaCode,check_code = :checkCode,card_cost = :cardCost,acct_bal = :acctBal,bat_no = :batNo,init_date = :initDate,init_oper = :initOper,state = :state,open_oper = :openOper,open_date = :openDate,valid_date = :validDate,last_date = :lastDate,remark = :remark  where card_no = :cardNo ";
  private static final String LIST_ALL_OFFLINE_ACCOUNT_CONVERT = "select  a.card_no,a.user_id,a.merchant_code,a.card_type,a.card_bin,a.area_code,a.check_code,a.card_cost,a.acct_bal,a.bat_no,a.init_date,a.init_oper,a.state,a.open_oper,a.open_date,a.valid_date,a.last_date,a.remark ,CASE when a.state = 0 THEN '未激活' when a.state = 1 THEN '激活' when a.state = 2 THEN '冻结' when a.state = 3 THEN '挂失' when a.state = 4 THEN '注销' end as stateDesc  from ms_offline_account a";
  private static final String ACTIVATE_OFFLINE_CARD = "update ms_offline_account set state=:state,open_oper=:openOper,open_date=:openDate where card_no=:cardNo";
  private static final String UPDATE_OFFLINE_ACCOUNT_BAL = "update ms_offline_account  set  acct_bal=acct_bal+:acctBal,last_date=:lastDate where card_no=:cardNo";
  
  public List<OfflineAccountVO> listAllOfflineAccount()
  {
    return queryForList("select  a.card_no,a.user_id,a.merchant_code, a.card_type,a.card_bin,a.area_code,a.check_code,a.card_cost,a.acct_bal,a.bat_no,a.init_date,a.init_oper,a.state,a.open_oper,a.open_date,a.valid_date,a.last_date,a.remark  from ms_offline_account as a  order  by  bat_no  desc ", null, OfflineAccountVO.class);
  }
  
  public int insertOfflineAccount(OfflineAccountVO offlineAccountVO)
  {
    return save("insert into ms_offline_account(card_no,user_id,merchant_code,card_type,card_bin,area_code,check_code,card_cost,acct_bal,bat_no,init_date,init_oper,state,open_oper,open_date,valid_date,last_date,remark)  values(:cardNo,:userId,:merchantCode,:cardType,:cardBin,:areaCode,:checkCode,:cardCost,:acctBal,:batNo,:initDate,:initOper,:state,:openOper,:openDate,:validDate,:lastDate,:remark )", offlineAccountVO);
  }
  
  public int activateCard(OfflineAccountVO offlineAccountVO)
    throws Exception
  {
    try
    {
      return save("update ms_offline_account set state=:state,open_oper=:openOper,open_date=:openDate where card_no=:cardNo", offlineAccountVO);
    }
    catch (Exception e)
    {
      LogUtil.error("激活离线卡异常，参数offlineAccountVO为：" + offlineAccountVO, e);
      throw e;
    }
  }
  
  public int[] batchUpateOfflineAccount(List<OfflineAccountVO> offlineAccountList)
  {
    return batchSave("update  ms_offline_account set user_id = :userId,card_type = :cardType,card_bin = :cardBin,area_code = :areaCode,check_code = :checkCode,card_cost = :cardCost,acct_bal = :acctBal,bat_no = :batNo,init_date = :initDate,init_oper = :initOper,state = :state,open_oper = :openOper,open_date = :openDate,valid_date = :validDate,last_date = :lastDate,remark = :remark  where card_no = :cardNo ", offlineAccountList);
  }
  
  public List<OfflineAccountVO> queryOfflineAccount(OfflineAccountVO offlineAccountVO)
  {
    String sql = "select  a.card_no,a.user_id,a.merchant_code, a.card_type,a.card_bin,a.area_code,a.check_code,a.card_cost,a.acct_bal,a.bat_no,a.init_date,a.init_oper,a.state,a.open_oper,a.open_date,a.valid_date,a.last_date,a.remark  from ms_offline_account as a  where 1 = 1 " + assemblyConditions(offlineAccountVO, "a", false);
    return queryForList(sql, offlineAccountVO, OfflineAccountVO.class);
  }
  
  public List<OfflineAccountVO> queryOfflineAccountAndConvert(OfflineAccountVO offlineAccountVO)
  {
    String sql = "select  a.card_no,a.user_id,a.merchant_code,a.card_type,a.card_bin,a.area_code,a.check_code,a.card_cost,a.acct_bal,a.bat_no,a.init_date,a.init_oper,a.state,a.open_oper,a.open_date,a.valid_date,a.last_date,a.remark ,CASE when a.state = 0 THEN '未激活' when a.state = 1 THEN '激活' when a.state = 2 THEN '冻结' when a.state = 3 THEN '挂失' when a.state = 4 THEN '注销' end as stateDesc  from ms_offline_account a where 1 = 1 " + assemblyConditions(offlineAccountVO, "a", false);
    return queryForList(sql, offlineAccountVO, OfflineAccountVO.class);
  }
  
  public boolean updatetOfflineAccountBal(OfflineAccountVO param)
    throws Exception
  {
    try
    {
      int n = save("update ms_offline_account  set  acct_bal=acct_bal+:acctBal,last_date=:lastDate where card_no=:cardNo", param);
      return n == 1;
    }
    catch (Exception e)
    {
      LogUtil.error("updatetOfflineAccountBal::更新离线卡记账余额异常，参数为：" + param, e);
      throw e;
    }
  }
}
